package com.jr.dao.impl;

import com.jr.dao.EnterpriseDao;
import com.jr.entry.Enterprise;
import com.jr.entry.User;
import com.jr.util.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class EnterpriseDaoImpl implements  EnterpriseDao {
    DBHelper db=new DBHelper();
    @Override
    public Enterprise selectEnterpriseByAccount(String account) {
        String sql=" select * from enterprise e inner join user u on(u.enterpriseId=e.id) where account=? ";
        return(Enterprise)((db.query(sql,Enterprise.class, account).get(0)));
    }

    @Override
    public Enterprise selectEnterpriseById(String id) {
        String sql="select * from enterprise where id=?  ";
        return(Enterprise)(db.query(sql,Enterprise.class,id).get(0));
    }

    @Override
    public Enterprise selectEnterpriseByName(String name) {
        String sql="select * from enterprise where name= ? ";
        return (Enterprise) (db.query(sql,Enterprise.class,name).get(0));
    }

    @Override
    public ArrayList<Enterprise> selectAllEnterprise() {
        String sql="select * from  enterprise";
        return db.query(sql,Enterprise.class);
    }


    @Override
    public boolean insertEnterprise(Enterprise enterprise) {
        String sql="INSERT into enterprise(name,SocialUniformCode,email,phone,address,scale,fax) VALUES(?,?,?,?,?,?,?)";
        return db.updateTable(sql,enterprise.getName(),enterprise.getSocialUniformCode(),enterprise.getEmail(),enterprise.getPhone(),enterprise.getAddress(),enterprise.getScale(),enterprise.getFax())==0?false:true;

    }
    @Override
    public ArrayList<Enterprise> selectEnterpriseByObscureName(String name) {
        String sql="select * from enterprise where name like ? ";
        return db.query(sql,Enterprise.class,"%"+name+"%");

    }

    @Override
    public ArrayList<Enterprise> selectAllEnterpriseNotInBlack(Integer id,String account) {
        String sql="SELECT * FROM `enterprise` where  ID not IN (SELECT supplierId FROM supplier_black_list WHERE enterpriseId = ? " +
                " union " +
                " SELECT enterpriseId " +
                " FROM user " +
                " WHERE account = ? )";
        return db.query(sql,Enterprise.class,id,account);
    }
}
